package cn.hairui.blog.controller;

import cn.hairui.blog.config.WebLog;
import cn.hairui.blog.constant.PubConstant;
import cn.hairui.blog.model.Album;
import cn.hairui.blog.model.AlbumImg;
import cn.hairui.blog.model.User;
import cn.hairui.blog.model.WebInfo;
import cn.hairui.blog.service.AlbumService;
import cn.hairui.blog.service.WebInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * <h2>请添加说明
 * </h2>
 * <ol>请添加详细说明
 * </ol>
 *
 * <h2>修改历史
 * </h2>
 * <ol>如有修改，请添加修改历史
 * </ol>
 *
 * </p>
 *
 * @author laoganbu
 * @version V1.0
 * @date 2019/7/5 17:31
 */
@Controller
public class AlbumController {

    @Autowired
    private WebInfoService webInfoService;

    @Autowired
    private AlbumService albumService;

    @RequestMapping(value = "album")
    @WebLog(description = "查看相册")
    public String album(HttpServletRequest request, Model model) {
        int pageSize = 6;//默认每页显示6条记录
        WebInfo webInfo = webInfoService.find(PubConstant.MY_INFO_ID);
        model.addAttribute("webinfo", webInfo);
        //最新上传
        AlbumImg albumImg = albumService.findLatestImg();
        model.addAttribute("newalbumimg", albumImg);
        Integer pageNum = null;
        String pageNumStr = request.getParameter("pageIndex");
        if (pageNumStr != null) {
            pageNum = Integer.parseInt(pageNumStr);
        }
        if (pageNum == null) {
            pageNum = 1;
        }

        PageHelper.startPage(pageNum, pageSize);
        List<Album> albumsList = null;
        User userinfo = (User) request.getSession().getAttribute(PubConstant.GLOBAL_SESSION_NAME);
        if (userinfo == null || !PubConstant.YES_NO_Y.equals(userinfo.getAdminflag())) {
            //普通用户或者游客   只展示公开相册userinfo.setSuperUser()
            albumsList = albumService.findGust();
        } else if (PubConstant.YES_NO_Y.equals(userinfo.getAdminflag()) && !PubConstant.YES_NO_Y.equals(userinfo.getSuperUser())) {
            //管理人员 但不是超级用户 可以查看公开和自己管理的未公开的相册
            albumsList = albumService.findManager(userinfo.getUsername());
        } else if (PubConstant.YES_NO_Y.equals(userinfo.getAdminflag()) && PubConstant.YES_NO_Y.equals(userinfo.getSuperUser())) {
            //超级用户 可以查看所有
            albumsList = albumService.list();
        }

        PageInfo<Album> pageInfo = new PageInfo<Album>(albumsList);

        Iterator albumsIterator = albumsList.iterator();
        List<Map> albums = new ArrayList<>();
        while (albumsIterator.hasNext()) {
            Map map = new HashMap();
            Album album = (Album) albumsIterator.next();

            Integer id = album.getId();
            String name = album.getName();
            String desc = album.getIntroduction();
            Integer num = albumService.countImg(id);

            map.put("id", id);
            map.put("name", name);
            map.put("cover", album.getShowImg());
            map.put("time", album.getCreateTime().substring(0, 10));

            map.put("num", num);
            map.put("desc", desc);
            map.put("location", album.getLocation());
            albums.add(map);
        }
        model.addAttribute("albums", albums);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageIndex", pageNum);
        model.addAttribute("pageSize", pageSize);
        return "album";
    }

    @RequestMapping(value = "albumimg")
    @WebLog(description = "查看相册照片内容")
    public String albumimg(HttpServletRequest request, int id, Model model) {

        WebInfo webInfo = webInfoService.find(PubConstant.MY_INFO_ID);
        model.addAttribute("webinfo", webInfo);

        User userinfo = (User) request.getSession().getAttribute(PubConstant.GLOBAL_SESSION_NAME);
        if (userinfo == null || !PubConstant.YES_NO_Y.equals(userinfo.getAdminflag())) {
            //普通用户或者游客 查询该相册是否展示
            Album album = albumService.find(id);
            if (!PubConstant.YES_NO_Y.equals(album.getShowFlag())) {
                //该相册不向游客和普通用户展示  提示权限不足
                return "redirect:/permissions";
            }
        } else if (PubConstant.YES_NO_Y.equals(userinfo.getAdminflag()) && !PubConstant.YES_NO_Y.equals(userinfo.getSuperUser())) {
            //管理人员 但不是超级用户 暂时不做限制

        } else if (PubConstant.YES_NO_Y.equals(userinfo.getAdminflag()) && PubConstant.YES_NO_Y.equals(userinfo.getSuperUser())) {
            //超级用户 无限制
        }
        List<AlbumImg> albumImgList = albumService.findImgsByAlbuId(id);
        model.addAttribute("albumImgs", albumImgList);
        return "albumimg";
    }

}
